package components;

import java.io.IOException;

import logger.Log;

import org.openqa.selenium.WebDriver;

import utils.Config;
import componentsAbstract.CompAbstract;

public class Label extends CompAbstract{

	String label="";
	Log log;
	
	
	public Label(WebDriver driver, String label) {
		this.driver = driver;
		this.classInfo = this.getClass().getCanonicalName();
		this.label=label;
		launchLog();
	}
	
	
	public boolean safeFindLabel() throws Exception
	{
		

		classInfo += " public boolean safeFindLabel()";
		int licz = 0;
		while (licz < Config.getInstance().getMaxTryCountAction() && !findLabel()) {
			Thread.sleep(Config.getInstance().getThreadSleepBeforeTryActionMilis());
			licz++;
			log.info("safeFindLabel()  ILOSC PROB: "+licz+"  Obiekt: "+this.getClass() +"  Label:  "+label);
		}
		
		if(licz==Config.getInstance().getMaxTryCountAction())
		{
			throw new Exception("safeClick() PRZEKROCZONO ILOSC PROB: "+licz+"  Obiekt: "+this.getClass() +"  getFieldName:  "+this.getFieldName());
		}
		
		
		return true;
	}
	
	private boolean findLabel()
	{
		if (driver.getPageSource().contains(label))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	private void launchLog()
	{
		try {
			if(log == null)
			{
				log = new Log(this.getClass());
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public String toString()
	{
		return label;
	}
	
}
